<?php
/**
 * Created by PhpStorm.
 * User: yuanbo
 * Date: 2018/8/20
 * Time: 下午11:15
 */

namespace app\admin\controller;


use think\Controller;

class MasterCourse extends Controller
{
    //**********大师课程列表***********
    public  function  courseList()
    {
        $courseList = db('master_course')
            ->join('master','master_course.master_id=master.master_id','left')
            ->select();
        $this->assign('courseList',$courseList);
        return $this->fetch('MasterCourse/courseList');
    }
    //**********修改大师课程**********
    public function  updateCourse()
    {
        //课程id
        $course_id = input('course_id');
        $course = db('master_course')
            ->join('master','master_course.master_id=master.master_id','left')->where('course_id',$course_id)
            ->find();
        $this->assign('course',$course);
        $masterList = db('master')->select();
        $this->assign('masterList',$masterList);
        return $this->fetch('MasterCourse/updateCourse');
    }
    //修改
    public  function  update()
    {
        //课程id
        $course_id = input('course_id');
        //课程名称
        $course_name = input('course_name');
        //大师名称id
        $master_id = input('master_name');
        //大师视频地址
        $course_video = input('course_video');
        //关注度
        $carevalue = input('carevalue');
        //课程海报
        $course_poster = request()->file('course_poster');

        $data = ['course_id'=>$course_id,'course_name'=>$course_name,'master_id'=>$master_id,
            'course_video'=>$course_video,'carevalue'=>$carevalue];
        if(!is_null($course_poster))
        {
            $info = $course_poster->rule('uniqid')->move('../public/static/masterCourse/uploads/pic');
            if($info)
            {
                $data['course_poster'] = 'uploads/pic/'.$info->getFilename();
                $rs = db('master_course')->where('course_id',$course_id)->update($data);
                if($rs)
                {
                    $this->success('上传文件成功，修改信息成功');
                }
                else
                {
                    $this->error('修改信息失败');
                }
            }
            else
            {
                //上传失败
                echo $info->getError();
                $this->error('上传文件失败');
            }

        }
        else
        {
            $rs = db('master_course')->where('course_id',$course_id)->update($data);
            if($rs)
            {
                $this->success('修改信息成功');
            }
            else
            {
                $this->error('修改信息失败');
            }
        }
    }

    //*********添加大师课程******
    public function  addCourse()
    {
        $masterList = db('master')->select();
        $this->assign('masterList',$masterList);
        return $this->fetch('MasterCourse/addCourse');
    }
    //添加
    public function add()
    {
        //课程名称
        $course_name = input('course_name');
        //大师名称id
        $master_id = input('master_name');
        //大师视频
        $course_video = request()->file('course_video');
        //关注度
        $carevalue = input('carevalue');
        //课程海报
        $course_poster = request()->file('course_poster');
        //创建时间
        $create_time = date('Y:m:d h:i:s');
        $data = ['course_id'=>null,'course_name'=>$course_name,'master_id'=>$master_id,
            'carevalue'=>$carevalue,'create_time'=>$create_time];
        if(!is_null($course_poster)||!is_null($course_video))
        {
         $videoinfo = $course_video->validate(['ext'=>'mp4,MP4'])->move('../public/static/img/masterCourse/uploads/video','');
         $picinfo = $course_poster->validate(['ext'=>'png,jpg'])->rule('uniqid')->move('../public/static/img/masterCourse/uploads/pic');
         if ($videoinfo&&$picinfo)
         {
             $data['course_video']= 'uploads/video/' . $videoinfo->getFilename();
             $data['course_poster']='uploads/pic/'.$picinfo->getFilename();
             $rs = db('master_course')->insert($data);
             if($rs)
             {
                 $this->success('上传文件成功，添加成功');
             }
             else
             {
                 $this->error('添加失败');
             }
         }
         else
         {
           if(!$videoinfo)
           {
               echo $course_video->getError();
               $this->error('上传视频失败，添加失败');
           }
           else if(!$picinfo)
           {
               echo  $course_poster->getError();
               $this->error('上传海报失败，添加失败');
           }
         }
        }
        else
        {
            $this->error('添加失败，无文件上传');
        }
    }


}